﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using League.Entity;
using League.DBUtility;
using System.Data;

namespace League.DataAccess
{
    public class LeagueTeamRankingDataAccess
    {
        public static List<LeagueTeamRanking> GetTeamRankingByRoundNumber(int roundNumber)
        {
            List<LeagueTeamRanking> result = new List<LeagueTeamRanking>();

            string sql = string.Format("SELECT * FROM LeagueTeamRanking WHERE RoundNumber = {0}", roundNumber);
            result = LeagueSQLEntityHelper<LeagueTeamRanking>.GetEntitySet(sql);

            return result;
        }

        public static LeagueTeamRanking Find(string leagueId, int roundNumber, string teamId)
        {
            string sql = string.Format("SELECT * FROM LeagueTeamRanking WHERE LeagueID='{0}' AND RoundNumber={1} AND TeamID='{2}'",
                leagueId, roundNumber, teamId);
            return LeagueSQLEntityHelper<LeagueTeamRanking>.GetEntity(sql);
        }

        public static int Update(LeagueTeamRanking leagueTeamRanking)
        {
            string sql = string.Format("UPDATE LeagueTeamRanking SET TeamRanking={0}, TeamMatchCount={1}, MatchWin={2}," +
                " MatchDraw={3}, MatchLose={4}, Goal={5}, Lose={6}, TeamPoint={7},UpdateTime='{8}' " +
                " WHERE LeagueID='{9}' AND RoundNumber={10} AND TeamID='{11}'", leagueTeamRanking.TeamRanking, 
                leagueTeamRanking.TeamMatchCount, leagueTeamRanking.MatchWin, leagueTeamRanking.MatchDraw, 
                leagueTeamRanking.MatchLose, leagueTeamRanking.Goal, leagueTeamRanking.Lose, 
                leagueTeamRanking.TeamPoint, System.DateTime.Now, leagueTeamRanking.LeagueID, 
                leagueTeamRanking.RoundNumber, leagueTeamRanking.TeamID);

            return LeagueSQLHelper.ExecuteNonQuery(CommandType.Text, sql);
        }

        public static int Insert(LeagueTeamRanking leagueTeamRanking)
        {
            string sql = string.Format("INSERT INTO LeagueTeamRanking(LeagueID,RoundNumber,TeamID,TeamRanking,TeamMatchCount," +
                " MatchWin,MatchDraw,MatchLose,Goal,Lose,TeamPoint) VALUES('{0}',{1},'{2}',{3},{4},{5},{6},{7},{8},{9},{10})", leagueTeamRanking.LeagueID,
                leagueTeamRanking.RoundNumber, leagueTeamRanking.TeamID, leagueTeamRanking.TeamRanking, leagueTeamRanking.TeamMatchCount,
                leagueTeamRanking.MatchWin, leagueTeamRanking.MatchDraw, leagueTeamRanking.MatchLose, leagueTeamRanking.Goal, leagueTeamRanking.Lose,
                leagueTeamRanking.TeamPoint);

            return LeagueSQLHelper.ExecuteNonQuery(CommandType.Text, sql);
        }
    }
}
